import { defineStore } from "pinia";
import type { User, UserText } from "@/types/user";
import { userlogin } from "@/api/user";
import type { LoginParams } from "@/api/types";
// import router from "@/router";
// import socket from "@/socket";
const useUsersStore = defineStore("users", {
  state: (): {
    users: User[];
    currentUser: User | null;
    texts: UserText[];
    token: string
    name: string
  } => ({
    users: [
      // {
      //   id: "1",
      //   name: "张三",
      //   avatar:
      //     "https://img0.baidu.com/it/u=3915743412,1295121707&fm=253&fmt=auto&app=120&f=JPEG?w=40&h=40",
      //   isGroup: false,
      // },
      // {
      //   id: "2",
      //   name: "李四",
      //   avatar:
      //     "https://img0.baidu.com/it/u=1025674513,2115761948&fm=253&fmt=auto&app=120&f=JPEG?w=40&h=40",
      //   isGroup: false,
      // },
      // {
      //   id: "3",
      //   name: "王五",
      //   avatar:
      //     "https://img0.baidu.com/it/u=3280176051,1411439140&fm=253&fmt=auto&app=120&f=JPEG?w=40&h=40",
      //   isGroup: false,
      // },
      // {
      //   id: "4",
      //   name: "群聊1",
      //   avatar:
      //     "https://q2.itc.cn/q_70/images03/20240714/5265b8330b054f4294314df029b3a530.jpeg?w=40&h=40",
      //   isGroup: true,
      //   list: ["1", "2", "3"],
      // },
    ],
    texts: [],
    currentUser: null,
    token: '',
    name: ''
  }),
  getters: {
    renderText() {
      const id = this.currentUser?.id;
      if (id) {
        const texts = this.texts.find((item: UserText) => item.id === id);
        return texts;
      }
    },
  },
  actions: {
    // 用户登录
    async login(data: LoginParams, name: string) {
      const res = await userlogin(data);
      console.log("res", res);
      if (res.code === 200) {
        sessionStorage.setItem("token", res.data);
        this.token = res.data;
        this.name = name
        // socket.emit("login", { name, isGroup: false });
        // router.push("/chat");
        return true;
      }
      return false;
    },
    // 添加用户列表
    addUsers(users: User[]) {
      this.users = this.users.concat(users);
    },
    // 添加用户
    addUser(user: User) {
      this.users.push(user);
    },
    // 删除用户
    deleteUser(id: string) {
      const index = this.users.findIndex(
        (user) => user.id === id || user.gid === id
      );
      if (index !== -1) {
        this.users.splice(index, 1);
      }
    },
    // 设置当前用户
    setCurrentUser(user: User) {
      this.currentUser = user;
    },
    // 用户插入
    insertBeforeUser(user: User, targetId: string) {
      // console.log("targetId", targetId);
      // console.log("id", user.id);
      this.users = this.users.filter((item: User) => {
        return item.id !== user.id;
      });
      let index = this.users.findIndex((item: User) => {
        return item.id === targetId;
      });
      console.log("index", index);
      if (index > -1) {
        this.users.splice(index, 0, user);
      } else {
        this.users.push(user);
      }
    },

    // 添加消息列表
    addTexts({
      id,
      msg,
    }: {
      id: string;
      msg: { isSelf: boolean; text: string };
    }) {
      const index = this.texts.findIndex((item: UserText) => item.id === id);
      if (index > -1) {
        this.texts[index].texts.push(msg);
      } else {
        this.texts.push({ id, texts: [msg] });
      }
    },
  },
});

export default useUsersStore;
